﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace Viewer.BusinessObject.BusinessEntities
{
    public partial class CHCommonCode : BusinessBase
    {
        DataClassesDataContext db = new DataClassesDataContext();
        CommonCode common = new CommonCode();
        public string TenMaCha { get; set; }
        public string TenCodeIdType { get; set; }
        public void aa()
        {

        }

        public void Save()
        {
            if (this.CodeID == 0)
            {
                this.Context.CHCommonCodes.InsertOnSubmit(this);

            }
            else
            {
                var oItem = GetCodeByID(this.CodeID);
                oItem.CodeIDCha = this.CodeIDCha;
                oItem.CodeIDType = this.CodeIDType;
                oItem.CodeKey = this.CodeKey;
                oItem.CodeString = this.CodeString;
                oItem.CodeValue = this.CodeValue;
                oItem.GhiChu = this.GhiChu;
            }

            this.Context.SubmitChanges();
        }

        public void Delete()
        {
            this.Context.CHCommonCodes.DeleteOnSubmit(this);
            this.Context.SubmitChanges();
        }

        public CHCommonCode GetCodeByID(int codeID)
        {
            return this.Context.CHCommonCodes.Where(m => m.CodeID == codeID).FirstOrDefault();
        }

        #region
        public void EditCommonCode(int codeId, string codeKey, string codeValue, int codeIdType, int codeIdCha, string ghiChu, string codeString)
        {
            List<CHCommonCode> lst = (List<CHCommonCode>)HttpContext.Current.Session["CHCommonCode"];
            CHCommonCode pc = lst.Where(a => a.CodeID.Equals(codeId)).SingleOrDefault();
            pc.CodeKey = codeKey;
            pc.GhiChu = ghiChu;
            pc.CodeString = codeString;
            pc.CodeValue = codeValue;
            pc.CodeIDType = codeIdType;
            pc.CodeIDCha = codeIdCha;
            pc.TenCodeIdType = pc.CodeIDType.HasValue ? common.LoadCommonCodeById(pc.CodeIDType.Value) : string.Empty;
            pc.TenMaCha = common.LoadCommonCodeById(pc.CodeIDCha);
        }
        public void DeleteCommonCode(int codeId)
        {
            List<CHCommonCode> lst = (List<CHCommonCode>)HttpContext.Current.Session["CHCommonCode"];
            CHCommonCode pc = lst.Where(a => a.CodeID.Equals(codeId)).SingleOrDefault();
        }

        public void UpdateCommonCode(List<CHCommonCode> commonObject)
        {
            foreach (CHCommonCode pc in commonObject)
            {
                var item = (from a in db.CHCommonCodes where a.CodeID.Equals(pc.CodeID) select a).FirstOrDefault();
                if (item == null)
                {
                    CHCommonCode itemNew = new CHCommonCode();
                    CopyCommonCode(pc, itemNew);
                    db.CHCommonCodes.InsertOnSubmit(itemNew);
                }
                else
                {
                    db.CHCommonCodes.DeleteOnSubmit(item);
                 
                }
                db.SubmitChanges();
            }
        }
        private void CopyCommonCode(CHCommonCode src, CHCommonCode des)
        {
            des.CodeIDCha = src.CodeIDCha;
            des.GhiChu = src.GhiChu;
            des.CodeIDCha = src.CodeIDCha;
            des.TenMaCha = src.TenMaCha;
            des.TenCodeIdType = src.TenCodeIdType;
            des.CodeKey = src.CodeKey;
            des.CodeString = src.CodeString;
            des.CodeValue = src.CodeValue;
        }

        public CHCommonCode GetCommonCode(string codeKey, string codeValue, int codeIdType, int codeIdCha, string ghiChu, string codeString)
        {
            var db = new DataClassesDataContext();
            CHCommonCode pc = new CHCommonCode();
            pc.CodeID = 0 - DateTime.Now.Second;
            pc.CodeString = codeString;
            pc.CodeKey = codeKey;
            pc.GhiChu = ghiChu;
            pc.CodeValue = codeValue;
            pc.CodeIDType = codeIdType;
            pc.CodeIDCha = codeIdCha;
            pc.TenCodeIdType = pc.CodeIDType.HasValue ? common.LoadCommonCodeById(pc.CodeIDType.Value) : string.Empty;
            pc.TenMaCha = common.LoadCommonCodeById(pc.CodeIDCha);
            return pc;
        }
        #endregion
    }
}